using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using viBook.Common;
using viBook.Filters;
using viBook.Helpers;
using viBook.Models;
using viBook.Repositories;

namespace viBook.Controllers
{
    public class ChapterCommentsController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IActivityRepository _activityRepository;
        private readonly IChapterCommentRepository _chapterCommentRepository;

        public ChapterCommentsController(
            IChapterCommentRepository chapterCommentRepository,
            IActivityRepository activityRepository)
        {
            _chapterCommentRepository = chapterCommentRepository;
            _activityRepository = activityRepository;
        }

        #endregion

        [HttpPost]
        [JsonAuthentication]
        public JsonResult Create(ChapterComment chapterComment)
        {
            if (ModelState.IsValid)
            {
                chapterComment.UserId = ExtendedIdentity.Id;
                _chapterCommentRepository.InsertOrUpdate(chapterComment);
                _chapterCommentRepository.Save();
                _activityRepository.CreateActivity(chapterComment, Constant.CommentedChapterType);
                _activityRepository.Save();
                return Json(new
                                {
                                    id=chapterComment.Id,
                                    success = true,
                                    createdDate = DateTime.Now.ConvertDateToString(),
                                    userImage = ExtendedIdentity.UserImage,
                                    userName = ExtendedIdentity.Name
                                });
            }
            return FailedFeedback;
        }

        [HttpPost, ActionName("Delete")]
        public JsonResult DeleteConfirmed(int id)
        {
            _chapterCommentRepository.Delete(id);
            _chapterCommentRepository.Save();
            _activityRepository.DeleteActivity(id, Constant.CommentedChapterType);
            _activityRepository.Save();
            return SuccessFeedback;
        }

        [HttpPost]
        [JsonAuthentication]
        public JsonResult Like(int id)
        {
            DataFeedback feedback = _chapterCommentRepository.VoteUp(id, ExtendedIdentity.Id);
            if (feedback.IsSuccess)
            {
                ChapterCommentLike chapterCommentLike = _activityRepository.FindChapterCommentLike(id,
                                                                                                   ExtendedIdentity.Id);
                 _activityRepository.CreateActivity(chapterCommentLike, Constant.LikedChapterCommentType);
                _activityRepository.Save();
                return SuccessFeedback;
            }
           
            return Json(new { success = false, message = feedback.Message });
        }
    

        [HttpPost]
        [JsonAuthentication]
        public JsonResult UnLike(int id)
        {
            DataFeedback feedback = _chapterCommentRepository.VoteDown(id, ExtendedIdentity.Id);
            if (feedback.IsSuccess)
            {
                ChapterCommentLike chapterCommentLike = _activityRepository.FindChapterCommentLike(id,
                                                                                                   ExtendedIdentity.Id);
                 _activityRepository.CreateActivity(chapterCommentLike, Constant.LikedChapterCommentType);
                _activityRepository.Save();
                return SuccessFeedback;
            }

            return Json(new { success = false, message = feedback.Message });
        }

        [HttpPost]
        [JsonAuthentication]
        public JsonResult ReportComment(int id)
        {
            _chapterCommentRepository.Report(id, ExtendedIdentity.Id);
            return SuccessFeedback;
        }

        [HttpPost]
        public JsonResult FetchNextCommentPage(int chapterId, int pageNumber)
        {
            List<ChapterComment> chapterComments =
                _chapterCommentRepository.FetchCommentsByPage(chapterId, pageNumber,ExtendedIdentity.Id).ToList();
            return Json(new
                            {
                                isSuccess = true,
                                comments = chapterComments.Select(c => new
                                                                    {
                                                                        id = c.Id,
                                                                        username = c.User.DisplayName,
                                                                        userImage = c.User.AvatarUrl,
                                                                        content = c.Content,
                                                                        createdDate =
                                                                        c.CreatedDate.ConvertDateToString(),
                                                                        like = c.Like,
                                                                        isReported = c.IsReported,
                                                                        isLiked = c.IsLiked,
                                                                        isAuthorized = c.IsAuthorized
                                                                    })
                            });
        }
    }
}